package o;

import com.huawei.appgallery.base.httpskit.PostField;
import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.json.JSONException;

public class bjo {
    private static final fup ˎ = fup.ॱ(RequestBody.HEAD_VALUE_CONTENT_TYPE_URLENCODED);
    private static final Charset ˏ = Charset.forName("UTF-8");

    public static fuw ˋ(bjh o_bjh, bjm o_bjm) {
        Writer outputStreamWriter;
        Throwable e;
        Writer writer;
        try {
            Map ˎ = ˎ(o_bjh);
            fxi o_fxi = new fxi();
            outputStreamWriter = new OutputStreamWriter(o_fxi.ˎ(), ˏ);
            try {
                if (!ˎ.isEmpty()) {
                    Set<Entry> entrySet = ˎ.entrySet();
                    int size = entrySet.size();
                    StringBuffer stringBuffer = new StringBuffer();
                    int i = size;
                    for (Entry entry : entrySet) {
                        String str = (String) entry.getKey();
                        String str2 = (String) entry.getValue();
                        outputStreamWriter.append(str + ContainerUtils.KEY_VALUE_DELIMITER + URLEncoder.encode(str2, "UTF-8"));
                        stringBuffer.append(str + ContainerUtils.KEY_VALUE_DELIMITER + str2);
                        size = i - 1;
                        if (size > 0) {
                            outputStreamWriter.append(ContainerUtils.FIELD_DELIMITER);
                            stringBuffer.append(ContainerUtils.FIELD_DELIMITER);
                        }
                        i = size;
                    }
                    outputStreamWriter.flush();
                    o_bjm.ˋ(stringBuffer.toString());
                }
                fuw create = fuw.create(ˎ, o_fxi.ʻॱ());
                if (outputStreamWriter == null) {
                    return create;
                }
                try {
                    outputStreamWriter.close();
                    return create;
                } catch (Throwable e2) {
                    o_bjm.ˏ("close writer IOException:", e2);
                    return create;
                }
            } catch (IllegalAccessException e3) {
                e = e3;
                writer = outputStreamWriter;
                try {
                    o_bjm.ˏ("IllegalAccessException:", e);
                    if (writer != null) {
                        try {
                            writer.close();
                        } catch (Throwable e4) {
                            o_bjm.ˏ("close writer IOException:", e4);
                        }
                    }
                    return null;
                } catch (Throwable th) {
                    e4 = th;
                    outputStreamWriter = writer;
                    if (outputStreamWriter != null) {
                        try {
                            outputStreamWriter.close();
                        } catch (Throwable e22) {
                            o_bjm.ˏ("close writer IOException:", e22);
                        }
                    }
                    throw e4;
                }
            } catch (UnsupportedEncodingException e5) {
                e4 = e5;
                try {
                    o_bjm.ˏ("UnsupportedEncodingException:", e4);
                    if (outputStreamWriter != null) {
                        try {
                            outputStreamWriter.close();
                        } catch (Throwable e42) {
                            o_bjm.ˏ("close writer IOException:", e42);
                        }
                    }
                    return null;
                } catch (Throwable th2) {
                    e42 = th2;
                    if (outputStreamWriter != null) {
                        outputStreamWriter.close();
                    }
                    throw e42;
                }
            } catch (IOException e6) {
                e42 = e6;
                o_bjm.ˏ("IOException:", e42);
                if (outputStreamWriter != null) {
                    try {
                        outputStreamWriter.close();
                    } catch (Throwable e422) {
                        o_bjm.ˏ("close writer IOException:", e422);
                    }
                }
                return null;
            } catch (JSONException e7) {
                e422 = e7;
                o_bjm.ˏ("JSONException:", e422);
                if (outputStreamWriter != null) {
                    try {
                        outputStreamWriter.close();
                    } catch (Throwable e4222) {
                        o_bjm.ˏ("close writer IOException:", e4222);
                    }
                }
                return null;
            }
        } catch (IllegalAccessException e8) {
            e4222 = e8;
            writer = null;
            o_bjm.ˏ("IllegalAccessException:", e4222);
            if (writer != null) {
                writer.close();
            }
            return null;
        } catch (UnsupportedEncodingException e9) {
            e4222 = e9;
            outputStreamWriter = null;
            o_bjm.ˏ("UnsupportedEncodingException:", e4222);
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
            return null;
        } catch (IOException e10) {
            e4222 = e10;
            outputStreamWriter = null;
            o_bjm.ˏ("IOException:", e4222);
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
            return null;
        } catch (JSONException e11) {
            e4222 = e11;
            outputStreamWriter = null;
            o_bjm.ˏ("JSONException:", e4222);
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
            return null;
        } catch (Throwable th3) {
            e4222 = th3;
            outputStreamWriter = null;
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
            throw e4222;
        }
    }

    public static Map<String, String> ˎ(bjh o_bjh) throws IllegalAccessException, JSONException {
        Map hashMap = new HashMap();
        Class cls = o_bjh.getClass();
        do {
            Field[] declaredFields = cls.getDeclaredFields();
            if (declaredFields.length > 0) {
                for (final Field field : declaredFields) {
                    if (field.isAnnotationPresent(PostField.class)) {
                        if (!field.isAccessible()) {
                            AccessController.doPrivileged(new PrivilegedAction() {
                                public Object run() {
                                    field.setAccessible(true);
                                    return null;
                                }
                            });
                        }
                        hashMap.put(field.getName(), ˊ(field, o_bjh));
                    }
                }
            }
            cls = cls.getSuperclass();
        } while (cls != Object.class);
        return hashMap;
    }

    private static String ˊ(Field field, Object obj) throws IllegalAccessException, JSONException {
        Class type = field.getType();
        Object obj2 = field.get(obj);
        if (type == Integer.TYPE || type == Integer.class) {
            return obj2 == null ? "0" : String.valueOf(obj2);
        } else {
            if (type == Double.TYPE || type == Double.class || type == Float.TYPE || type == Float.class) {
                return obj2 == null ? "0" : String.valueOf(obj2);
            } else {
                if (type == Boolean.TYPE || type == Boolean.class) {
                    return obj2 == null ? "false" : String.valueOf(obj2);
                } else {
                    if (type == Long.TYPE || type == Long.class) {
                        return obj2 == null ? "0" : String.valueOf(obj2);
                    } else {
                        if (type == String.class) {
                            return obj2 == null ? HwAccountConstants.NULL : String.valueOf(obj2);
                        } else {
                            return bjt.ˋ(field.get(obj));
                        }
                    }
                }
            }
        }
    }
}
